package com.sky.controller.user;


import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Api(tags = "C端-用户端相关接口")
@Slf4j
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;


    // 微信登录
    @ApiOperation("微信登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("微信用户登录，授权码：{}", userLoginDTO.getCode());
        // 微信接口服务获取 openId
        User user = userService.wxLogin(userLoginDTO.getCode());
        // 为用户设置 jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        // 在 jwt 中设置有效载荷（自定义信息）
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
                                        jwtProperties.getUserTtl(),
                                        claims);
        UserLoginVO userLoginVO = new UserLoginVO(user.getId(), user.getOpenid(), token);
        return Result.success(userLoginVO);
    }

    // 查询分类

}
